frames reset
clear all 
	
cap frame drop results

frames create results str15(weight) year trim p1 p2 p3 p4 p5 p6 p8 p10 p11 p12 p39 p40 p50 p51 p52 p89 

* Prediction

use data/icenes8321_weights, clear
clonevar w_xswt_5 = w_xswt 
*clonevar w_xswt_100 = w_xswt 

foreach w in xswt A D AMD E AMDE {
	foreach h in 5 {

		svyset [pweight=w_`w'_`h']

			svy: tab year party if party!=0, row

			matrix freq = e(N_pop)*e(Prop)
			mata: st_matrix("freq", (st_matrix("freq") :/ rowsum(st_matrix("freq"))))


	frame post results ("`w'") (1983) (`h') (freq[1,1]) (freq[1,2]) (freq[1,3]) (freq[1,4]) (freq[1,5]) (freq[1,6]) (freq[1,7]) (freq[1,8]) (freq[1,9]) (freq[1,10]) (freq[1,11]) (freq[1,12]) (freq[1,13]) (freq[1,14]) (freq[1,15]) (freq[1,16]) 
	frame post results ("`w'") (1987) (`h') (freq[2,1]) (freq[2,2]) (freq[2,3]) (freq[2,4]) (freq[2,5]) (freq[2,6]) (freq[2,7]) (freq[2,8]) (freq[2,9]) (freq[2,10]) (freq[2,11]) (freq[2,12]) (freq[2,13]) (freq[2,14]) (freq[2,15]) (freq[2,16]) 
	frame post results ("`w'") (1991) (`h') (freq[3,1]) (freq[3,2]) (freq[3,3]) (freq[3,4]) (freq[3,5]) (freq[3,6]) (freq[3,7]) (freq[3,8]) (freq[3,9]) (freq[3,10]) (freq[3,11]) (freq[3,12]) (freq[3,13]) (freq[3,14]) (freq[3,15]) (freq[3,16]) 
	frame post results ("`w'") (1995) (`h') (freq[4,1]) (freq[4,2]) (freq[4,3]) (freq[4,4]) (freq[4,5]) (freq[4,6]) (freq[4,7]) (freq[4,8]) (freq[4,9]) (freq[4,10]) (freq[4,11]) (freq[4,12]) (freq[4,13]) (freq[4,14]) (freq[4,15]) (freq[4,16]) 
	frame post results ("`w'") (1999) (`h') (freq[5,1]) (freq[5,2]) (freq[5,3]) (freq[5,4]) (freq[5,5]) (freq[5,6]) (freq[5,7]) (freq[5,8]) (freq[5,9]) (freq[5,10]) (freq[5,11]) (freq[5,12]) (freq[5,13]) (freq[5,14]) (freq[5,15]) (freq[5,16]) 
	frame post results ("`w'") (2003) (`h') (freq[6,1]) (freq[6,2]) (freq[6,3]) (freq[6,4]) (freq[6,5]) (freq[6,6]) (freq[6,7]) (freq[6,8]) (freq[6,9]) (freq[6,10]) (freq[6,11]) (freq[6,12]) (freq[6,13]) (freq[6,14]) (freq[6,15]) (freq[6,16]) 
	frame post results ("`w'") (2007) (`h') (freq[7,1]) (freq[7,2]) (freq[7,3]) (freq[7,4]) (freq[7,5]) (freq[7,6]) (freq[7,7]) (freq[7,8]) (freq[7,9]) (freq[7,10]) (freq[7,11]) (freq[7,12]) (freq[7,13]) (freq[7,14]) (freq[7,15]) (freq[7,16]) 
	frame post results ("`w'") (2009) (`h') (freq[8,1]) (freq[8,2]) (freq[8,3]) (freq[8,4]) (freq[8,5]) (freq[8,6]) (freq[8,7]) (freq[8,8]) (freq[8,9]) (freq[8,10]) (freq[8,11]) (freq[8,12]) (freq[8,13]) (freq[8,14]) (freq[8,15]) (freq[8,16]) 
	frame post results ("`w'") (2013) (`h') (freq[9,1]) (freq[9,2]) (freq[9,3]) (freq[9,4]) (freq[9,5]) (freq[9,6]) (freq[9,7]) (freq[9,8]) (freq[9,9]) (freq[9,10]) (freq[9,11]) (freq[9,12]) (freq[9,13]) (freq[9,14]) (freq[9,15]) (freq[9,16]) 
frame post results ("`w'") (2016) (`h') (freq[10,1]) (freq[10,2]) (freq[10,3]) (freq[10,4]) (freq[10,5]) (freq[10,6]) (freq[10,7]) (freq[10,8]) (freq[10,9]) (freq[10,10]) (freq[10,11]) (freq[10,12]) (freq[10,13]) (freq[10,14]) (freq[10,15]) (freq[10,16]) 
frame post results ("`w'") (2017) (`h') (freq[11,1]) (freq[11,2]) (freq[11,3]) (freq[11,4]) (freq[11,5]) (freq[11,6]) (freq[11,7]) (freq[11,8]) (freq[11,9]) (freq[11,10]) (freq[11,11]) (freq[11,12]) (freq[11,13]) (freq[11,14]) (freq[11,15]) (freq[11,16]) 
frame post results ("`w'") (2021) (`h') (freq[12,1]) (freq[12,2]) (freq[12,3]) (freq[12,4]) (freq[12,5]) (freq[12,6]) (freq[12,7]) (freq[12,8]) (freq[12,9]) (freq[12,10]) (freq[12,11]) (freq[12,12]) (freq[12,13]) (freq[12,14]) (freq[12,15]) (freq[12,16]) 

	}
}
	
		

************************************
************  Fetching benchmarks  *
************************************

frame create temp 
frame change temp 
	use "popdata/targets/party_1983-2021.dta", clear
	
	drop if party==0
	bys year (party): egen N = total(n)
	gen voteshare=n/N
	
	keeporder year party voteshare
	
frame change results

***********************************
************  Prediction Error  *
***********************************

reshape long p, i(weight year trim) j(party)
	
frlink m:1 year party, frame(temp)
frget voteshare, from(temp)

gen err=abs(p-voteshare)
drop temp 

* DROP OTHER PARTIES
drop if party==89

 *** Error for parties
	save data/party_errors, replace	


reshape wide p voteshare err, i(weight year trim) j(party)

egen mae=rowmean(err*)

***********************************
************  Comparison  *
***********************************

frame copy results aa
	frame change aa
	
	keeporder year weight mae 
	
	reshape wide mae, i(weight) j(year)
	egen mae_avg=rowmean(mae*)
	sort mae_avg 	

	
	reshape long mae, i(weight) j(year)
	save data/weights_predictions, replace
	
*********************************************************************************************
* NOW WE FIND THE UNWEIGHTED ERROR FOR YOUNG VOTERS, PRIMARY EDUCATION AND TURNOUT	
*********************************************************************************************

*********************************************************************************************
* EDUCATION
*********************************************************************************************

		cap frame drop results_educ

		frames create results_educ year p1 p2 p3 

		***************************************
		************  Calculate sample shares *
		***************************************

		use temp/sampledata, clear
				svyset [pweight=xswt]

				svy: tab year educ, row

					matrix freq = e(N_pop)*e(Prop)
					mata: st_matrix("freq", (st_matrix("freq") :/ rowsum(st_matrix("freq"))))

				frame post results_educ (1983) (freq[1,1]) (freq[1,2]) (freq[1,3]) 
				frame post results_educ (1987) (freq[2,1]) (freq[2,2]) (freq[2,3]) 
				frame post results_educ (1991) (freq[3,1]) (freq[3,2]) (freq[3,3]) 
				frame post results_educ (1995) (freq[4,1]) (freq[4,2]) (freq[4,3]) 
				frame post results_educ (1999) (freq[5,1]) (freq[5,2]) (freq[5,3]) 
				frame post results_educ (2003) (freq[6,1]) (freq[6,2]) (freq[6,3]) 
				frame post results_educ (2007) (freq[7,1]) (freq[7,2]) (freq[7,3]) 
				frame post results_educ (2009) (freq[8,1]) (freq[8,2]) (freq[8,3]) 
				frame post results_educ (2013) (freq[9,1]) (freq[9,2]) (freq[9,3]) 
				frame post results_educ (2016) (freq[10,1]) (freq[10,2]) (freq[10,3])  
				frame post results_educ (2017) (freq[11,1]) (freq[11,2]) (freq[11,3])  
				frame post results_educ (2021) (freq[12,1]) (freq[12,2]) (freq[12,3])  

		************************************
		************  Fetching benchmarks  *
		************************************
		cap frame drop temp
		frame create temp 
		frame change temp 
			use "popdata/targets/educ_1983-2021.dta", clear
			
			bys year (educ): egen N = total(n)
			gen share=n/N
			
			keeporder year educ share
			
		frame change results_educ

		***********************************
		************  Prediction Error  *
		***********************************

		reshape long p, i(year) j(educ)
			
		frlink m:1 year educ, frame(temp)
		frget share, from(temp)

		gen err=abs(p-share)
		drop temp 

		reshape wide p share err, i(year) j(educ)

		egen mae=rowmean(err*)

		save data/educ_errors, replace	

*********************************************************************************************
* AGE GROUPS
*********************************************************************************************

		cap frame drop results_age

		frames create results_age year p1 p2 p3 p4

		***************************************
		************  Calculate sample shares *
		***************************************

		use temp/sampledata, clear
				svyset [pweight=xswt]

				svy: tab year agecat, row

					matrix freq = e(N_pop)*e(Prop)
					mata: st_matrix("freq", (st_matrix("freq") :/ rowsum(st_matrix("freq"))))

				frame post results_age (1983) (freq[1,1]) (freq[1,2]) (freq[1,3])   (freq[1,4])    
				frame post results_age (1987) (freq[2,1]) (freq[2,2]) (freq[2,3]) (freq[2,4]) 
				frame post results_age (1991) (freq[3,1]) (freq[3,2]) (freq[3,3]) (freq[3,4]) 
				frame post results_age (1995) (freq[4,1]) (freq[4,2]) (freq[4,3]) (freq[4,4]) 
				frame post results_age (1999) (freq[5,1]) (freq[5,2]) (freq[5,3]) (freq[5,4]) 
				frame post results_age (2003) (freq[6,1]) (freq[6,2]) (freq[6,3]) (freq[6,4]) 
				frame post results_age (2007) (freq[7,1]) (freq[7,2]) (freq[7,3]) (freq[7,4]) 
				frame post results_age (2009) (freq[8,1]) (freq[8,2]) (freq[8,3]) (freq[8,4]) 
				frame post results_age (2013) (freq[9,1]) (freq[9,2]) (freq[9,3]) (freq[9,4]) 
				frame post results_age (2016) (freq[10,1]) (freq[10,2]) (freq[10,3]) (freq[10,4])  
				frame post results_age (2017) (freq[11,1]) (freq[11,2]) (freq[11,3]) (freq[11,4])  
				frame post results_age (2021) (freq[12,1]) (freq[12,2]) (freq[12,3]) (freq[12,4])  

		************************************
		************  Fetching benchmarks  *
		************************************

		cap frame drop temp
		frame create temp 
		frame change temp 
			use "popdata/targets/agecat_1983-2021.dta", clear
			
			bys year (agecat): egen N = total(n)
			gen share=n/N
			
			keeporder year agecat share
			
		frame change results_age

		***********************************
		************  Prediction Error  *
		***********************************

		reshape long p, i(year) j(agecat)			
		frlink m:1 year agecat, frame(temp)
		frget share, from(temp)

		gen err=abs(p-share)
		drop temp 

		reshape wide p share err, i(year) j(agecat)

		egen mae=rowmean(err*)

		save data/age_errors, replace	


*********************************************************************************************
* TURNOUT
*********************************************************************************************

		cap frame drop results_turnout

		frames create results_turnout year p0

		***************************************
		************  Calculate sample shares *
		***************************************

		use temp/sampledata, clear
				svyset [pweight=xswt]

				svy: tab year party, row

					matrix freq = e(N_pop)*e(Prop)
					mata: st_matrix("freq", (st_matrix("freq") :/ rowsum(st_matrix("freq"))))

				frame post results_turnout (1983) (freq[1,1])   
				frame post results_turnout (1987) (freq[2,1]) 
				frame post results_turnout (1991) (freq[3,1]) 
				frame post results_turnout (1995) (freq[4,1]) 
				frame post results_turnout (1999) (freq[5,1]) 
				frame post results_turnout (2003) (freq[6,1]) 
				frame post results_turnout (2007) (freq[7,1]) 
				frame post results_turnout (2009) (freq[8,1]) 
				frame post results_turnout (2013) (freq[9,1]) 
				frame post results_turnout (2016) (freq[10,1])  
				frame post results_turnout (2017) (freq[11,1])  
				frame post results_turnout (2021) (freq[12,1])  

		************************************
		************  Fetching benchmarks  *
		************************************

		cap frame drop temp
		frame create temp 
		frame change temp 
			use "popdata/targets/party_1983-2021.dta", clear
	
			bys year (party): egen N = total(n)
			gen share=n/N
			keep if party==0
			keeporder year party share
			
		frame change results_turnout

		***********************************
		************  Prediction Error  *
		***********************************

		reshape long p, i(year) j(party)			
		frlink m:1 year party, frame(temp)
		frget share, from(temp)

		gen err=abs(p-share)
		drop temp 

		rename err mae

		save data/turnout_errors, replace	
		

*********************************************************************************************
* CAPITAL AREA
*********************************************************************************************

		cap frame drop results_area

		frames create results_area year p0

		***************************************
		************  Calculate sample shares *
		***************************************

		use temp/sampledata, clear
				svyset [pweight=xswt]
				
				recode district (3 4 6 11 12=1) (nonm=0), gen(capitalarea)
				svy: tab year capitalarea, row

					matrix freq = e(N_pop)*e(Prop)
					mata: st_matrix("freq", (st_matrix("freq") :/ rowsum(st_matrix("freq"))))

				frame post results_area (1983) (freq[1,1])   
				frame post results_area (1987) (freq[2,1]) 
				frame post results_area (1991) (freq[3,1]) 
				frame post results_area (1995) (freq[4,1]) 
				frame post results_area (1999) (freq[5,1]) 
				frame post results_area (2003) (freq[6,1]) 
				frame post results_area (2007) (freq[7,1]) 
				frame post results_area (2009) (freq[8,1]) 
				frame post results_area (2013) (freq[9,1]) 
				frame post results_area (2016) (freq[10,1])  
				frame post results_area (2017) (freq[11,1])  
				frame post results_area (2021) (freq[12,1])  

		************************************
		************  Fetching benchmarks  *
		************************************

		cap frame drop temp
		frame create temp 
		frame change temp 
			use "popdata/targets/district_1983-2021.dta", clear
	
			recode district (3 4 6 11 12=1) (nonm=0), gen(capitalarea)
			collapse (sum) n, by(year capitalarea)
			
			bys year (capitalarea): egen N = total(n)
			gen share=n/N
			keep if capitalarea==0
			keeporder year capitalarea share
			
		frame change results_area

		***********************************
		************  Prediction Error  *
		***********************************

		reshape long p, i(year) j(capitalarea)			
		frlink m:1 year capitalarea, frame(temp)
		frget share, from(temp)

		gen err=abs(p-share)
		drop temp 

		rename err mae

		save data/capitalarea_errors, replace	

		
*********************************************************************************************
* FEMALE
*********************************************************************************************

		cap frame drop results_sex

		frames create results_sex year p0

		***************************************
		************  Calculate sample shares *
		***************************************

		use temp/sampledata, clear
				svyset [pweight=xswt]
				
				svy: tab year male, row

					matrix freq = e(N_pop)*e(Prop)
					mata: st_matrix("freq", (st_matrix("freq") :/ rowsum(st_matrix("freq"))))

				frame post results_sex (1983) (freq[1,1])   
				frame post results_sex (1987) (freq[2,1]) 
				frame post results_sex (1991) (freq[3,1]) 
				frame post results_sex (1995) (freq[4,1]) 
				frame post results_sex (1999) (freq[5,1]) 
				frame post results_sex (2003) (freq[6,1]) 
				frame post results_sex (2007) (freq[7,1]) 
				frame post results_sex (2009) (freq[8,1]) 
				frame post results_sex (2013) (freq[9,1]) 
				frame post results_sex (2016) (freq[10,1])  
				frame post results_sex (2017) (freq[11,1])  
				frame post results_sex (2021) (freq[12,1])  

		************************************
		************  Fetching benchmarks  *
		************************************

		cap frame drop temp
		frame create temp 
		frame change temp 
			use "popdata/targets/male_1983-2021.dta", clear
				
			bys year: egen N = total(n)
			gen share=n/N
			keep if male==0
			keeporder year male share
			
		frame change results_sex

		***********************************
		************  Prediction Error  *
		***********************************

		reshape long p, i(year) j(male)			
		frlink m:1 year male, frame(temp)
		frget share, from(temp)

		gen err=abs(p-share)
		drop temp 

		rename err mae

		save data/female_errors, replace	
		